package server.java.autoduel.Cars;

import java.io.Serializable;
import java.nio.ByteBuffer;

import server.java.autoduel.Network.IPacketizable;
import server.java.autoduel.Network.OPCODES;
import server.java.autoduel.Network.Util;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedObject;

/**
 * Represents a car's chassis
 * 
 * @author Ian Axelrod
 * @author Liz Ha
 * @author Andrew Muldowney
 * @author Edward Futch
 */
public class Chassis implements ManagedObject, Serializable, IPacketizable {
	private static final long serialVersionUID = -6478306325120007460L;

	/**
	 * The name of the chassis
	 */
	private String name;

	/**
	 * The weight modifier of the chassis
	 */
	private int weightMod;

	/**
	 * The cost modifier of the chassis
	 */
	private int costMod;

	/**
	 * The relative location of the image used for the chassis sprite
	 */
	private String image;

	/**
	 * Constructs a chassis object
	 * 
	 * @param name
	 *            the name of the chassis
	 * @param weightMod
	 *            the weight modifier of the chassis
	 * @param costMod
	 *            the cost modifier of the chassis
	 */
	public Chassis(String name, int weightMod, int costMod, String image) {
		this.name = name;
		this.weightMod = weightMod;
		this.costMod = costMod;
		this.image = image;
	}

	/**
	 * Creates a copy of this chassis
	 * 
	 * @return a copy of the chassis
	 */
	public Chassis copy() {
		return new Chassis(this.name, this.weightMod, this.costMod, this.image);
	}

	@Override
	public ByteBuffer encodePacket(OPCODES opcode) {
		final byte[] bytes = new byte[1 + // opcode
				this.name.length() + 1 + // delim
				4 + // costMod
				4 + // weightMod
				this.image.length() + 1]; // delim
		final ByteBuffer buf = ByteBuffer.wrap(bytes);

		buf.put((byte) OPCODES.ORD_UPDCHASSESLIST.ordinal());
		buf.put(this.name.getBytes());
		buf.put(Util.PKT_DLM);
		buf.putInt(this.costMod);
		buf.putInt(this.weightMod);
		buf.put(this.image.getBytes());
		buf.put(Util.PKT_DLM);

		buf.flip();

		return buf;
	}

	/**
	 * @return the costMod
	 */
	public int getCostMod() {
		return this.costMod;
	}

	/**
	 * @return the image
	 */
	public String getImage() {
		return this.image;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @return the weightMod
	 */
	public int getWeightMod() {
		return this.weightMod;
	}

	/**
	 * @param costMod
	 *            the costMod to set
	 */
	public void setCostMod(int costMod) {
		AppContext.getDataManager().markForUpdate(this);
		this.costMod = costMod;
	}

	/**
	 * @param image
	 *            the image to set
	 */
	public void setImage(String image) {
		AppContext.getDataManager().markForUpdate(this);
		this.image = image;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		AppContext.getDataManager().markForUpdate(this);
		this.name = name;
	}

	/**
	 * @param weightMod
	 *            the weightMod to set
	 */
	public void setWeightMod(int weightMod) {
		AppContext.getDataManager().markForUpdate(this);
		this.weightMod = weightMod;
	}
}